﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Clinica_Frba.AbmdeRol
{
    public partial class edit : Form
    {
        public edit()
        {
            InitializeComponent();
        }
        int idr;
        public edit(int id)
        {
            InitializeComponent();
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            idr = id;
            SqlCommand nombre = new SqlCommand("SELECT NOMBRE_ROL FROM DUNDER_MIFFLIN.ROL WHERE ID_ROL=" + id, myConnection);
            SqlDataReader sr = nombre.ExecuteReader();
            sr.Read();
            textBox1.Text = sr.GetString(0);
            sr.Close();
        }
        private void edit_Load(object sender, EventArgs e)
        {
            string comando = "SELECT ID_FUNC,DESC_FUNC FROM DUNDER_MIFFLIN.FUNCIONALIDAD";
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            SqlCommand consulta = new SqlCommand(comando, myConnection);
            SqlDataReader da = consulta.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("id_func");
            dt.Columns.Add("desc_func");
            dt.Load(da);
            listBox1.DisplayMember = "desc_func";
            listBox1.ValueMember = "id_func";
            listBox1.DataSource = dt;
            listBox1.SelectedIndex = 0;
            SqlCommand roles2=new SqlCommand("SELECT DUNDER_MIFFLIN.ROL_FUNCIONALIDAD.ID_FUNC as idf,DUNDER_MIFFLIN.FUNCIONALIDAD.DESC_FUNC as df FROM DUNDER_MIFFLIN.ROL_FUNCIONALIDAD LEFT JOIN DUNDER_MIFFLIN.FUNCIONALIDAD ON DUNDER_MIFFLIN.FUNCIONALIDAD.ID_FUNC=DUNDER_MIFFLIN.ROL_FUNCIONALIDAD.ID_FUNC WHERE DUNDER_MIFFLIN.ROL_FUNCIONALIDAD.ID_ROL= "+idr,myConnection);
            SqlDataReader da2 = roles2.ExecuteReader();
            while (da2.Read()){
                listBox2.Items.Add(da2.GetString(1));
                }
            }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            SqlCommand upd = new SqlCommand("UPDATE DUNDER_MIFFLIN.ROL SET ESTADO='H' WHERE ID_ROL=" + idr, myConnection);
            upd.ExecuteNonQuery();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i, idfu;
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            if (textBox1.Text == "") { return; }
            SqlCommand beg = new SqlCommand("BEGIN TRAN ed",myConnection);
            beg.ExecuteNonQuery();
            try
            {

                SqlCommand upd = new SqlCommand("UPDATE DUNDER_MIFFLIN.ROL SET NOMBRE_ROL='" + textBox1.Text + "' WHERE ID_ROL=" + idr, myConnection);
                upd.ExecuteNonQuery();
                SqlCommand borr = new SqlCommand("DELETE FROM DUNDER_MIFFLIN.ROL_FUNCIONALIDAD WHERE ID_ROL="+idr,myConnection);
                borr.ExecuteNonQuery();
                for (i = 0; i < listBox2.Items.Count; i++)
                {
                    SqlCommand idf = new SqlCommand("SELECT ID_FUNC FROM DUNDER_MIFFLIN.FUNCIONALIDAD WHERE DESC_FUNC='" + listBox2.GetItemText(listBox2.Items[i]) + "'", myConnection);
                    SqlDataReader func = idf.ExecuteReader();
                    func.Read();
                    idfu = Convert.ToInt32(func.GetValue(0));
                    func.Close();
                    SqlCommand insert2 = new SqlCommand("INSERT INTO DUNDER_MIFFLIN.ROL_FUNCIONALIDAD(ID_ROL,ID_FUNC) VALUES(" + idr + "," + idfu + ")", myConnection);
                    insert2.ExecuteNonQuery();
                } 
                SqlCommand comm = new SqlCommand("COMMIT TRAN ed", myConnection);
                comm.ExecuteNonQuery();
                MessageBox.Show("Insercion ok", "Edicion de roles");
                   
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Edicion de roles");
                SqlCommand rb = new SqlCommand("ROLLBACK TRAN ed", myConnection);
                rb.ExecuteNonQuery();
            }
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count != 0)
            {
                bool ver = true;
                int i;

                for (i = 0; i < listBox2.Items.Count; i++)
                {
                    if (listBox2.GetItemText(listBox2.Items[i]) == listBox1.GetItemText(listBox1.SelectedItem)) { ver = false; }
                }
                if (ver)
                {

                    listBox2.Items.Add(listBox1.GetItemText(listBox1.SelectedItem));

                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count != 0)
            {
                listBox2.Items.Remove(listBox2.SelectedItem);
                if (listBox2.Items.Count > 0) { listBox2.SelectedIndex = 0; }
            }
        }
    }
}
